package com.you.redisqueue.task;

import com.alibaba.fastjson.JSON;
import com.you.redisqueue.constant.RedisKey;
import com.you.redisqueue.consumer.RedisConsumer;
import com.you.redisqueue.producer.RedisProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 *
 * 业务处理类 定时拉取redis上的队列
 * @Author: weiwei.you
 * @Date: 2020-11-13 20:14
 */

@Component
@Slf4j
public class BusinessDeal implements Runnable {

    @Autowired
    private RedisConsumer redisConsumer;

    @Autowired
    private RedisProducer redisProducer;


    /**
     * 轮询器，每1秒请求一次拉取
     **/
    @Scheduled(fixedDelay = 1000)
    @Override
    public void run() {
        Object o = redisConsumer.getData(RedisKey.QUEUE);
        if(ObjectUtils.isEmpty(o)){
            return;
        }
        deal(o);
    }

    /**
     * 处理业务逻辑
     * @param o
     */
    private void deal(Object o){

        try {

            // TODO 处理业务逻辑

        }catch (Exception e){
            log.error("此次任务失败 失败的对象为：{}",e,JSON.toJSONString(o));
            redisProducer.putData(RedisKey.FAIL_QUEUE, o);
        }


    }
}
